<?php
/**
 * Author: Creatint <creatint@163.com> <http://creatint.yotaku.cn>
 */
namespace Page\Model;
use Think\Model;
class AddressModel extends Model{
    /**
     * 前台通讯录
     */
    public $show = '';
    public function hselect($where=array(),$order='') {
        //筛选
        $nickname = I('get.nickname','','strip_tags');
        if(!empty($nickname)) {
            echo 'here';
            $wheren['nickname'] = array('like','%'.$nickname.'%');
        }

        $address = I('get.address','','strip_tags');
        if(!empty($address)) {
            $wheren['address'] = array('like','%'.$address.'%');
        }
        $mobile = I('get.mobile','','strip_tags');
        if(!empty($mobile)) {
            $wheren['mobile'] = array('like','%'.$mobile.'%');
        }
        $qq = I('get.qq','','strip_tags');
        if(!empty($qq)) {
            $wheren['qq'] = array('like','%'.$qq.'%');
        }
        $cs = I('get.cs','','strip_tags');
        if(!empty($cs)) {
            $wheren['cs'] = array('like','%'.$cs.'%');
        }
        $qg = I('get.qg','','strip_tags');
        if(!empty($qg)) {
            $wheren['qg'] = array('like','%'.$qg.'%');
        }
        $company = I('get.company','','strip_tags');
        if(!empty($company)) {
            $wheren['company'] = array('like','%'.$company.'%');
        }

        $province = I('get.province','','strip_tags');
        if(!empty($province)) {
            $wheren['province'] = array('like','%'.$province.'%');
        } else {
            unset($_SESSION['province']);
        }

        $city = I('get.city','','strip_tags');
        if(!empty($city)) {
            $wheren['city'] = array('like','%'.$city.'%');
        }






//        if(empty($nickname) && empty($address) && empty($mobile) && empty($qq) && empty($cs) && empty($qg) && empty($company) && empty($province) && empty($city)) {
//            $wheren['nickname'] = array('like','');
//        } else {
//            $wheren['_logic'] = 'or';
//        }

        trace($wheren,'wheren');
        $getprovince = I('get.province','','strip_tags');
        if(!empty($getprovince)) {
            if($getprovince == '全部') {
                //$_SESSION['province'] = '';
                unset($_SESSION['province']);
            } else {
                $_SESSION['province'] = $getprovince;
            }
        }
        if($wheren){
            $where['_complex'] = $wheren;
        }
        if(!empty($_SESSION['province'])) {
            $where['province'] = $_SESSION['province'];
        }
        //最新出售
        $cs_new = I('get.cs_new','','strip_tags');
        if($cs_new == 1){
            $time = time();
            $start = $time-(30*24*60*60);
            $where['updatetime'] = array('between',array($start,$time));
        }
        //最新求购
        $qg_new = I('get.qg_new','','strip_tags');
        if($qg_new == 1){
            $time = time();
            $start = $time-(30*24*60*60);
            $where['updatetime'] = array('between',array($start,$time));
        }
        //最新加入同行
        $add_new = I('get.add_new','','strip_tags');
        if($add_new == 1){
            $time = time();
            $start = $time-(7*24*60*60); // 2天
            $where['paytime'] = array('between',array($start,$time));
            $order = $order.'ping asc , id desc';
        }else{
            //$year = date('Y');
			$year = 2016;
            $string = $year."0101";
            $yeartime = strtotime($string);

            $string = ($year+2)."0101";
            $nextyeartime = strtotime($string);
            $where['paytime'] = array('between',array($yeartime,$nextyeartime));
            $order = $order.'ping asc , id desc';
        }




        $list = $this->where($where)->order($order)->page($_GET['p'].',25')->select();
        trace($where,'where');
        $count      = $this->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show       = $Page->show();// 分页显示输出
        $this->show = $show;
        trace($show,'shop');
        trace($this->getLastSql(),'sql');
        return $list;
    }

    /**
     * 新加入同行
     */
    public function hselectnow($where=array(),$order='') {
        $time = time();
        $start = $time-(7*24*60*60); // 2天
        $where['paytime'] = array('between',array($start,$time));
        $order = $order.'ping asc , id desc';
        $list = $this->where($where)->order($order)->select();
        return $list;
    }

}
